package com.ruoyi.business.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.business.domain.AppCustomerBill;
import com.ruoyi.business.domain.AppTask;
import com.ruoyi.business.enums.CustomerBillMoneyType;
import com.ruoyi.business.mapper.AppCustomerBillMapper;
import com.ruoyi.business.mapper.AppCustomerMapper;
import com.ruoyi.business.mapper.AppTaskMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AppTaskOrderMapper;
import com.ruoyi.business.domain.AppTaskOrder;
import com.ruoyi.business.service.IAppTaskOrderService;

/**
 * 任务订单Service业务层处理
 * 
 * @author CuiYingJie
 * @date 2020-12-03
 */
@Service
public class AppTaskOrderServiceImpl implements IAppTaskOrderService 
{
    @Autowired
    private AppTaskOrderMapper appTaskOrderMapper;
    @Autowired
    private AppTaskMapper appTaskMapper;
    @Autowired
    private AppCustomerMapper appCustomerMapper;
    @Autowired
    private AppCustomerBillMapper appCustomerBillMapper;

    /**
     * 查询任务订单
     * 
     * @param id 任务订单ID
     * @return 任务订单
     */
    @Override
    public AppTaskOrder selectAppTaskOrderById(Long id)
    {
        return appTaskOrderMapper.selectAppTaskOrderById(id);
    }

    /**
     * 查询任务订单列表
     * 
     * @param appTaskOrder 任务订单
     * @return 任务订单
     */
    @Override
    public List<AppTaskOrder> selectAppTaskOrderList(AppTaskOrder appTaskOrder)
    {
        return appTaskOrderMapper.selectAppTaskOrderList(appTaskOrder);
    }


    /**
     * 管理员审核任务订单
     */
    @Override
    public int checkTaskOrder(Map<String, String> data) {

        int updateResult = 0;
        String status = data.get("status");
        /*
          审核不通过
         */
        if ("3".equals(status)){
            updateResult = appTaskOrderMapper.checkTaskOrder(data);
            if (updateResult != 1){
                return updateResult;
            }
        }

        /*
          审核通过
         */
        if (!"2".equals(status)){
            return updateResult;
        }

        AppTaskOrder thisAppTaskOrder = appTaskOrderMapper.selectAppTaskOrderById(Long.valueOf(data.get("taskLogId")));
        if (thisAppTaskOrder.getStatus() != 1){
            return updateResult;
        }

        AppTask thisAppTask = appTaskMapper.selectAppTaskById(thisAppTaskOrder.getTaskId());
        if (thisAppTask.getTaskNum() - 1 < 0){
            return updateResult;
        }

        updateResult = appTaskMapper.finishATask(thisAppTask.getId());
        if (updateResult != 1){
            return updateResult;
        }

        updateResult = appTaskOrderMapper.checkTaskOrder(data);
        if (updateResult != 1){
            return updateResult;
        }

        updateResult = appCustomerMapper.updatePriceByCustomerId(thisAppTaskOrder.getCustomerId(), thisAppTaskOrder.getPrice());
        if (updateResult != 1){
            return updateResult;
        }

        AppCustomerBill appCustomerBill = new AppCustomerBill();
        appCustomerBill.setCustomerId(thisAppTaskOrder.getCustomerId());
        appCustomerBill.setMoney(thisAppTaskOrder.getPrice());
        appCustomerBill.setMoneyType(CustomerBillMoneyType.REWARD.getCode());
        appCustomerBill.setMsg(thisAppTask.getTaskName() + "审核通过, 发放奖励" + thisAppTaskOrder.getPrice()+ "元");
        updateResult = appCustomerBillMapper.insertAppCustomerBill(appCustomerBill);
        return updateResult;
    }

    /**
     * 撤回已审核成功订单
     */
    @Override
    public int backCheckTaskOrder(Map<String, String> data) {
        int updateResult = 0;
        AppTaskOrder thisAppTaskOrder = appTaskOrderMapper.selectAppTaskOrderById(Long.valueOf(data.get("taskLogId")));
        if (thisAppTaskOrder.getStatus() != 2){
            return updateResult;
        }

        AppTask thisAppTask = appTaskMapper.selectAppTaskById(thisAppTaskOrder.getTaskId());
        if (thisAppTask.getTaskNum() == null){
            return updateResult;
        }

        updateResult = appTaskMapper.backATask(thisAppTask.getId());
        if (updateResult != 1){
            return updateResult;
        }

        updateResult = appTaskOrderMapper.backCheckTaskOrder(data);
        if (updateResult != 1){
            return updateResult;
        }

        updateResult = appCustomerMapper.backUpdatePriceByCustomerId(thisAppTaskOrder.getCustomerId(), thisAppTaskOrder.getPrice());
        if (updateResult != 1){
            return updateResult;
        }

        AppCustomerBill appCustomerBill = new AppCustomerBill();
        appCustomerBill.setCustomerId(thisAppTaskOrder.getCustomerId());
        appCustomerBill.setMoney(thisAppTaskOrder.getPrice());
        appCustomerBill.setMoneyType(CustomerBillMoneyType.REWARD.getCode());
        appCustomerBill.setMsg(thisAppTask.getTaskName() + "任务奖励撤回, 撤回原因: " + data.get("msg"));
        updateResult = appCustomerBillMapper.insertAppCustomerBill(appCustomerBill);
        return updateResult;
    }

    /**
     * 统计
     * @return
     */
    @Override
    public Integer countNotCheckTaskOrder() {
        return appTaskOrderMapper.countNotCheckTaskOrder();
    }
}
